<template>
  <div class="questions-randoms">
    <el-card>
      <!-- 表单 -->
      <el-form label-width="80px" inline>
        <el-form-item label="关键字">
          <el-input v-model="queryInfo.keyword" placeholder="根据编号搜索" @click="onSearch"></el-input>
        </el-form-item>

        <el-form-item class="form-btn">
          <el-button size="small" @click="clearInfo">清除</el-button>
          <el-button size="small" type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
      </el-form>

      <!-- 提示信息 -->
      <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false"> </el-alert>
      <!-- 表格 -->
      <template>
        <el-table :data="randomsList">
          <el-table-column label="编号" prop="id" width="220px"> </el-table-column>
          <el-table-column label="题型" prop="questionType" width="80px">
            <template slot-scope="scope">
              <span v-if="scope.row.questionType === '2'">多选</span>
              <span v-else-if="scope.row.questionType === '1'">单选</span>
              <span v-else>简答</span>
            </template>
          </el-table-column>
          <el-table-column label="题目编号" width="220px">
            <template slot-scope="scope">
              <div class="text" v-for="(item, index) in scope.row.questionIDs" :key="index" @click="show(item)">
                {{ item.number }}
              </div>
            </template>
          </el-table-column>

          <el-table-column label="录入时间" width="180px">
            <template slot-scope="scope">
              {{ scope.row.addTime | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="答题时间(s)" prop="totalSeconds" width="110px"> </el-table-column>
          <el-table-column label="正确率(%)" prop="accuracyRate" width="110px"> </el-table-column>
          <el-table-column label="录入人" prop="userName" width="110px"> </el-table-column>
          <el-table-column label="操作" width="80px">
            <template slot-scope="scope">
              <el-button type="danger" plain circle icon="el-icon-delete" size="small" @click="removeRandomsById(scope.row)"> </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination class="pager" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[20, 30, 50, 100]" :page-size="queryInfo.pagesize" layout=" prev,  pager, next,sizes, jumper" :total="total"> </el-pagination>
    </el-card>

    <!-- 对话框 -->

    <randoms-preview ref="randomsPreview" :data="diaglogId" />
  </div>
</template>

<script>
import { randoms as getRandoms, removeRandoms } from "@/api/hmmm/questions.js";
import RandomsPreview from "../components/randoms-preview";
export default {
  name: "questionsRandoms",
  components: { RandomsPreview },
  data() {
    return {
      randomsList: [],
      queryInfo: {
        keyword: null,
        page: 1,
        pagesize: 20,
      },
      total: 0,

      diaglogId: {},
    };
  },

  created() {
    this.getRandomsList();
  },
  methods: {
    // 获取数据
    async getRandomsList() {
      const { data } = await getRandoms(this.queryInfo);
      // console.log(data);
      this.randomsList = data.items;
      this.total = data.counts;
    },

    handleSizeChange(newSize) {
      this.queryInfo.page = 1;
      this.queryInfo.pagesize = newSize;
      this.getRandomsList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getRandomsList();
    },
    // 清除
    async removeRandomsById(question) {
      await this.$confirm("此操作将永久删除该题组, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });

      await removeRandoms({ id: question.id });
      // this.randomsList.splice(id, 1);
      this.$message.success("删除成功！");
      this.getRandomsList();
    },
    // 清空表单数据
    clearInfo() {
      this.queryInfo.keyword = null;
      this.getRandomsList();
    },
    // 搜索
    onSearch() {
      this.queryInfo.page = 1;
      this.getRandomsList();
    },
    show(diaglogId) {
      this.diaglogId = diaglogId;
      this.$nextTick(() => {
        this.$refs.randomsPreview.open();
      });
    },
  },
};
</script>

<style scoped rel="stylesheet/scss" lang="scss">
.questions-randoms {
  padding: 10px 15px;
  .form-btn {
    float: right;
  }
  .el-alert {
    margin-bottom: 15px;
  }
  .pager {
    margin-top: 15px;
    text-align: right;
  }
  .text {
    color: #409eff;
  }
}
</style>
